package com.hm.health.Utils;

import android.util.Log;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yxx on 7/19/16.
 */
public class TimeUtils {
    private static final String TAG = "TimeUtils";

    static public String convertTime(long time) {
        Date date = new Date(time);
        Format format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        Log.d(TAG, format.format(date));
        return format.format(date);
    }

    static public String timeDifference(long start, long end) {
        int time = (int) ((end - start) / 1000);
        String ret;
        if (time / 3600 > 1) {
            ret = Integer.toString(time / 3600) + "h";
        } else if (time / 60 > 1) {
            ret = Integer.toString(time / 60) + "m";
        } else {
            ret = Integer.toString(time) + "s";
        }
        return ret;
    }

    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date strToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String dateToStr(Date date) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String str = format.format(date);
        return str;
    }

    /**
     * sconds to time
     * @param s
     * @return
     */
    public static String SecondsToTime(long s) {
        int hour = (int) (s / 3600);
        int minute = (int) ((s % 3600) / 60);
        int sec = (int) (s - hour * 3600 - minute * 60);
        return String.format("%02d:%02d:%02d", hour, minute, sec);
    }

    public static String minutesToTime(long minutes) {
        int hour = (int) (minutes / 60);
        int minute = (int) (minutes % 60);
        return String.format("%02d:%02d", hour, minute);
    }
}
